<?php

namespace app\index\controller;

use think\Cache;
use think\Controller;
use think\Db;
use think\Loader;
use think\Request;
use think\Session;
use think\Validate;
use tree\Tree;

class Index extends Controller
{
    public function getUserList()
    {
        $input = Request()->param();

        $user_id = $input['user_id'];

        if (empty($user_id)) apiReturn(2, 'ID缺失');

        $shop_ids = Db::name('shop_contact')->where('contact_id', $user_id)->column('shop_id');
        if (empty($shop_ids)) apiReturn(2, '客服无管理门店');

         $list = Db::name('user')->field('id,username,contact_id')->where('user_type', 1)->whereIn('contact_id', [0, $user_id])->whereIn('up_shop_user_id', $shop_ids)->select();

        apiReturn(1, '成功', $list);
    }

    /**
     * 生成省市联动数据文件
     * */
    public function createRegionFile()
    {
        $region_list = Db::name('province')->field('province_id id,name')->order('province_id asc')->select();
        if ($region_list) {
            foreach ($region_list as &$v) {
                $city_list = Db::name('city')->field('city_id id,name,province_id pid')->where(['province_id' => $v['id']])->order('city_id asc')->select();
                foreach($city_list as &$val){
                    $area_list = Db::name('area')->field('area_id id,name,city_id pid')->where(['city_id' => $val['id']])->order('area_id asc')->select();
                    $val['area_list'] = $area_list ? : [];
                }
                $v['city_list'] = $city_list ? : [];
            }
        }

        file_put_contents(RUNTIME_PATH . 'region.json', json_encode($region_list, JSON_UNESCAPED_UNICODE));
    }
}
